package cn.ctodb.cms.service;

import cn.ctodb.cms.domain.CmsTag;
import cn.ctodb.cms.repository.CmsTagRepository;
import cn.ctodb.cms.repository.search.CmsTagSearchRepository;
import cn.ctodb.cms.service.dto.CmsTagDTO;
import cn.ctodb.cms.service.mapper.CmsTagMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing CmsTag.
 */
@Service
@Transactional
public class CmsTagService {

    private final Logger log = LoggerFactory.getLogger(CmsTagService.class);
    
    @Inject
    private CmsTagRepository cmsTagRepository;

    @Inject
    private CmsTagMapper cmsTagMapper;

    @Inject
    private CmsTagSearchRepository cmsTagSearchRepository;

    /**
     * Save a cmsTag.
     *
     * @param cmsTagDTO the entity to save
     * @return the persisted entity
     */
    public CmsTagDTO save(CmsTagDTO cmsTagDTO) {
        log.debug("Request to save CmsTag : {}", cmsTagDTO);
        CmsTag cmsTag = cmsTagMapper.cmsTagDTOToCmsTag(cmsTagDTO);
        cmsTag = cmsTagRepository.save(cmsTag);
        CmsTagDTO result = cmsTagMapper.cmsTagToCmsTagDTO(cmsTag);
        cmsTagSearchRepository.save(cmsTag);
        return result;
    }

    /**
     *  Get all the cmsTags.
     *  
     *  @return the list of entities
     */
    @Transactional(readOnly = true) 
    public List<CmsTagDTO> findAll() {
        log.debug("Request to get all CmsTags");
        List<CmsTagDTO> result = cmsTagRepository.findAll().stream()
            .map(cmsTagMapper::cmsTagToCmsTagDTO)
            .collect(Collectors.toCollection(LinkedList::new));

        return result;
    }

    /**
     *  Get one cmsTag by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Transactional(readOnly = true) 
    public CmsTagDTO findOne(Long id) {
        log.debug("Request to get CmsTag : {}", id);
        CmsTag cmsTag = cmsTagRepository.findOne(id);
        CmsTagDTO cmsTagDTO = cmsTagMapper.cmsTagToCmsTagDTO(cmsTag);
        return cmsTagDTO;
    }

    /**
     *  Delete the  cmsTag by id.
     *
     *  @param id the id of the entity
     */
    public void delete(Long id) {
        log.debug("Request to delete CmsTag : {}", id);
        cmsTagRepository.delete(id);
        cmsTagSearchRepository.delete(id);
    }

    /**
     * Search for the cmsTag corresponding to the query.
     *
     *  @param query the query of the search
     *  @return the list of entities
     */
    @Transactional(readOnly = true)
    public List<CmsTagDTO> search(String query) {
        log.debug("Request to search CmsTags for query {}", query);
        return StreamSupport
            .stream(cmsTagSearchRepository.search(queryStringQuery(query)).spliterator(), false)
            .map(cmsTagMapper::cmsTagToCmsTagDTO)
            .collect(Collectors.toList());
    }
}
